package com.example.yichuangyun.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

/**
 * ViewHolder基类
 */
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mViews;
    private Context mContext;

    public RecyclerViewHolder(Context context, View itemView) {
        super(itemView);
        mContext = context;
        mViews = new SparseArray<>();
    }

    public static RecyclerViewHolder get(Context context, ViewGroup parent, int layoutId) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        return new RecyclerViewHolder(context, itemView);
    }

    /**
     * 通过id获取view
     *
     * @param viewId 控件id
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            // 直接从ItemView中找
            view = itemView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 设置TextView文本
     */
    public RecyclerViewHolder setText(int viewId, CharSequence text) {
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }


    /**
     * 设置TextView文本
     *
     * @param viewId     控件的id
     * @param text       控件展示内容
     * @param resourceId 资源样式
     * @param textColor  展示文本的颜色
     * @return
     */
    public RecyclerViewHolder setText(int viewId, CharSequence text, int resourceId, String textColor) {
        TextView tv = getView(viewId);
        tv.setBackgroundResource(resourceId);
        tv.setTextColor(Color.parseColor(textColor));
        tv.setText(text);
        return this;
    }

    /**
     * 设置字体颜色
     *
     * @param viewId    控件的id
     * @param textColor 文本颜色
     * @return
     */
    public RecyclerViewHolder setTextColor(int viewId, int textColor) {
        TextView tv = getView(viewId);
        tv.setTextColor(textColor);
        return this;
    }

    /**
     * 设置View的Visibility
     */
    public RecyclerViewHolder setViewVisibility(int viewId, int visibility) {
        getView(viewId).setVisibility(visibility);
        return this;
    }

    /**
     * 设置View是否可点击
     */
    public RecyclerViewHolder setViewEnable(int viewId, boolean isEnable) {
        getView(viewId).setEnabled(isEnable);
        return this;
    }

    /**
     * 设置ImageView的资源
     */
    public RecyclerViewHolder setImageResource(int viewId, int resourceId) {
        ImageView imageView = getView(viewId);
        imageView.setImageResource(resourceId);
        return this;
    }

    /**
     * 设置ImageView的网络url
     */
    public RecyclerViewHolder setImageUrl(int viewId, String url) {
        ImageView imageView = getView(viewId);
        Glide.with(mContext).load(url).apply(
                new RequestOptions()
//                .placeholder(R.mipmap.icon_fun1)
        ).into(imageView);
        return this;
    }

    /**
     * 设置条目点击事件
     */
    public RecyclerViewHolder setOnInClickListener(int viewId, Object o, View.OnClickListener listener) {
        View view = getView(viewId);
        view.setTag(o);
        view.setOnClickListener(listener);
        return this;
    }

    public RecyclerViewHolder setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return this;
    }
}
